<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <div class="login-header">
        <h1>EverWin Admin</h1>
        <div v-if="isAuthenticated" class="auth-notice">
          <p>您已登录，如需重新登录请先登出</p>
          <div class="auth-buttons">
            <Button @click="goToDashboard" severity="success" size="small">进入系统</Button>
            <Button @click="handleLogout" severity="secondary" size="small">重新登录</Button>
          </div>
        </div>
      </div>
      
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <InputText 
            id="username" 
            v-model="form.username" 
            placeholder="请输入用户名"
            required
            :class="{ 'error': errors.username }"
          />
          <div v-if="errors.username" class="error-message">{{ errors.username }}</div>
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <InputText 
            id="password" 
            v-model="form.password" 
            type="password"
            placeholder="请输入密码"
            required
            :class="{ 'error': errors.password }"
          />
          <div v-if="errors.password" class="error-message">{{ errors.password }}</div>
        </div>
        
        <div class="form-group remember-me">
          <div class="checkbox-wrapper">
            <input type="checkbox" id="remember" v-model="rememberMe">
            <label for="remember">记住我</label>
          </div>
          <a href="#" class="forgot-password">忘记密码？</a>
        </div>
        
        <div class="form-group form-actions">
          <Button type="submit" :loading="loading" class="w-full">
            登录
          </Button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue'
import { useRouter } from 'vue-router'
// 使用相对路径替代别名路径
import { useUserStore } from '../../store/modules/user'
import InputText from 'primevue/inputtext'
import Button from 'primevue/button'

// 初始化 store 和路由
const router = useRouter()
const userStore = useUserStore()

// 认证状态
const isAuthenticated = ref(userStore.isAuthenticated)

// 监听userStore的认证状态变化
watch(() => userStore.isAuthenticated, (newValue) => {
  isAuthenticated.value = newValue
})

// 表单数据
const form = reactive({
  username: 'admin',
  password: '123456'
})

// 记住我选项
const rememberMe = ref(false)

// 错误信息
const errors = reactive<Record<string, string>>({})

// 加载状态
const loading = ref(false)

// 处理登录
const handleLogin = async () => {
  // 设置加载状态
  loading.value = true
  
  try {
    // 暂时不调用登录接口，直接设置为登录成功
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 500))
    
    // 直接设置登录状态，不调用接口
    userStore.token = 'mock-token-' + Date.now()
    userStore.isAuthenticated = true
    userStore.userInfo = {
      id: '1',
      username: 'admin',
      nickname: '管理员',
      email: 'admin@example.com',
      avatar: '',
      roles: ['admin'],
      permissions: ['*'],
      createTime: new Date().toISOString(),
      updateTime: new Date().toISOString()
    }
    
    // 保存到本地存储
    localStorage.setItem('token', userStore.token)
    
    // 更新本地状态
    isAuthenticated.value = true
    
    // 直接跳转到首页
    router.push('/dashboard')
    
    // 原来的登录逻辑（已注释）
    // 重置错误信息
    // Object.keys(errors).forEach(key => delete errors[key])
    
    // 表单验证
    // let isValid = true
    
    // if (!form.username.trim()) {
    //   errors.username = '请输入用户名'
    //   isValid = false
    // }
    
    // if (!form.password.trim()) {
    //   errors.password = '请输入密码'
    //   isValid = false
    // }
    
    // if (!isValid) return
    
    // 调用登录接口
    // const success = await userStore.login(form.username, form.password)
    // 
    // if (success) {
    //   // 登录成功，更新本地状态
    //   isAuthenticated.value = userStore.isAuthenticated
    // } else {
    //   throw new Error('登录失败')
    // }
  } catch (error: any) {
    // 处理登录失败
    console.error('登录失败:', error)
    
    // 根据错误信息设置提示
    if (error.message) {
      alert(error.message)
    } else {
      alert('登录失败，请稍后重试')
    }
  } finally {
    // 重置加载状态
    loading.value = false
  }
}

// 登出处理
const handleLogout = () => {
  userStore.logout()
  isAuthenticated.value = false
}

// 跳转到dashboard
const goToDashboard = () => {
  router.push('/dashboard')
}

</script>

<style scoped>
/* 全局样式重置 */
* {
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
  animation: gradientAnimation 15s ease infinite;
}

/* 背景装饰 */
.login-container::before,
.login-container::after {
  content: '';
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.3;
}

.login-container::before {
  width: 400px;
  height: 400px;
  background: rgba(255, 255, 255, 0.2);
  top: -100px;
  right: -100px;
  animation: float 8s ease-in-out infinite;
}

.login-container::after {
  width: 300px;
  height: 300px;
  background: rgba(255, 255, 255, 0.15);
  bottom: -100px;
  left: -100px;
  animation: float 10s ease-in-out infinite reverse;
}

.login-form-wrapper {
  width: 100%;
  max-width: 420px;
  min-width: 320px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 40px 30px;
  margin: 20px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transform: translateY(0);
  transition: transform 0.3s ease;
}

.login-form-wrapper:hover {
  transform: translateY(-5px);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}


.login-header h1 {
  margin: 0 0 10px 0;
  font-size: 28px;
  color: #333;
  font-weight: 600;
}

.login-header p {
  margin: 0;
  color: #666;
  font-size: 16px;
  font-weight: 400;
}

.login-form {
  width: 100%;
}

.form-group {
  margin-bottom: 24px;
  position: relative;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
  font-size: 14px;
  transition: color 0.3s ease;
}

.form-group .p-inputtext {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
  background-color: white;
}

.form-group .p-inputtext:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-group .p-inputtext:hover {
  border-color: #cbd5e0;
}

.form-group .error {
  border-color: #ff4d4f;
}

.form-group .error:focus {
  box-shadow: 0 0 0 3px rgba(255, 77, 79, 0.1);
}

.error-message {
  color: #ff4d4f;
  font-size: 12px;
  margin-top: 6px;
  display: block;
  animation: fadeIn 0.3s ease;
}

.remember-me {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.checkbox-wrapper {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.checkbox-wrapper input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin-right: 8px;
  accent-color: #667eea;
}

.checkbox-wrapper label {
  margin: 0;
  font-size: 14px;
  color: #666;
  cursor: pointer;
}

.forgot-password {
  color: #667eea;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #764ba2;
  text-decoration: underline;
}

.form-actions {
  margin-top: 40px;
}

.form-actions .p-button {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  text-transform: none;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.form-actions .p-button:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
}

.form-actions .p-button:active:not(:disabled) {
  transform: translateY(0);
}

.form-actions .p-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 动画 */
@keyframes gradientAnimation {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

@keyframes float {
  0% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(10px, 10px);
  }
  100% {
    transform: translate(0, 0);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 认证状态提示样式 */
.auth-notice {
  margin-top: 15px;
  padding: 15px;
  background-color: rgba(40, 167, 69, 0.1);
  border: 1px solid rgba(40, 167, 69, 0.3);
  border-radius: 6px;
  text-align: center;
}

.auth-notice p {
  margin: 0 0 12px 0;
  color: #155724;
  font-size: 16px;
  font-weight: 500;
}

.auth-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
}


/* 响应式设计 */
@media (max-width: 480px) {
  .login-form-wrapper {
    margin: 10px;
    padding: 30px 20px;
    max-width: calc(100vw - 20px);
  }
  
  .login-header h1 {
    font-size: 24px;
  }
  
  .auth-buttons {
    flex-direction: column;
    gap: 8px;
  }
  
  .auth-buttons .p-button {
    width: 100%;
  }
}

@media (max-width: 320px) {
  .login-form-wrapper {
    padding: 20px 15px;
  }
  
  .login-header h1 {
    font-size: 20px;
  }
}
</style>